package com.attack.web;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.cxf.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.attack.manager.ProductManager;
import com.attack.model.ProductModel;
import com.common.component.Pagination;
import com.common.utils.DateUtil;
import com.framework.model.IData;
import com.framework.model.impl.DataMap;
import com.framework.web.BaseController;

@Controller
@RequestMapping(value = "/management/product")
public class ProductController extends BaseController{

	 private static String modelUrl="/management/product/";
	 
    @Autowired
	private ProductManager  productManager;

    /**
     * 功能：页面分页列表
     * @param vo
     * @param param
     * @return
     */
	@RequestMapping("pageList")
	public ModelAndView pageList(@ModelAttribute("page") Pagination vo,@ModelAttribute("productModel") ProductModel param) {
		ModelAndView modelView = new ModelAndView(modelUrl+"list");
		List<ProductModel> dataList = productManager.getPageObjectList(vo, param);
		modelView.addObject("vo", vo);
		modelView.addObject("param", param);
		modelView.addObject("objList", dataList);
		return modelView;
	}
	
	/**
	 * 功能：初始化添加页面
	 * @param productModel
	 * @return
	 */
	@RequestMapping("initAdd")
	public ModelAndView initAdd(@ModelAttribute("productModel") ProductModel productModel) {
		ModelAndView modelView = new ModelAndView(modelUrl+"add");
		log.info("add is page");
		modelView.addObject("vo",productModel);
		return modelView;
	}
	
	/**
	 * 功能：初始化编辑页面
	 * @param intID
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/{intID}/initEdit")
	public ModelAndView initEdit(@PathVariable("intID") String intID) {
		ModelAndView modelView = new ModelAndView(modelUrl+"update");
		IData dataMap = new DataMap();
		dataMap.put("intID", intID);
		ProductModel productModel = (ProductModel)productManager.getObjectInfo(dataMap);
		modelView.addObject("vo",productModel);
		return modelView;
	}
	
	
	/**
	 * 功能：执行插入操作
	 * @param productModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public Object add(HttpServletResponse response,@ModelAttribute("productModel") ProductModel productModel) {
		try{
			int opNum = productManager.insertObjectAndGetID(productModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行更新操作
	 * @param productModel
	 * @return
	 */
	@ResponseBody
	@RequestMapping("update")
	public Object update(HttpServletResponse response,@ModelAttribute("productModel") ProductModel productModel) {
		try{
			int opNum =productManager.updateObject(productModel);
			if(opNum>0){
				return super.jsonReturn(response,200);
			}else{
				return super.jsonReturn(response,300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return super.jsonReturn(response,300,e.getMessage());
		}
	}
	
	/**
	 * 功能：执行单记录删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/{intID}/delete")
	public String delete(HttpServletResponse response,@PathVariable("intID") int intID) {
		try{
			IData dataMap = new DataMap();
			dataMap.put("intID", intID);
			int opNum =productManager.deleteObject(dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：执行批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deleteAll")
	public Object deleteAll(HttpServletResponse response,HttpServletRequest request) {
		try{
			String ids = request.getParameter("delids");
			IData dataMap = new DataMap();
			dataMap.put("ids", ids);
			int opNum =productManager.deleteObject("deleteAllObject",dataMap);
			if(opNum>0){
				return super.jsonReturn(response,200,false);
			}else{
				return super.jsonReturn(response,300,false);
			}
		} catch (Exception e) {
			return super.jsonReturn(response,300,e.getMessage(),false);
		}
	}
	
	/**
	 * 功能：根据产品、投资金额、期限查询查询信息
	 * @param response
	 * @param request
	 */
	@ResponseBody
	@RequestMapping("/loadProduct")
	public void loadProduct(HttpServletResponse response,HttpServletRequest request){
		String productCode = request.getParameter("productCode");
		String periodDay = request.getParameter("periodDay");
		String totalMoney = request.getParameter("totalMoney");
		JSONObject objJson = new JSONObject();
		if(!(StringUtils.isEmpty(productCode)&&StringUtils.isEmpty(periodDay)&&StringUtils.isEmpty(periodDay))){
			IData dataMap = new DataMap();
			dataMap.put("productCode",productCode);
			dataMap.put("periodDay", periodDay);
			dataMap.put("totalMoney",totalMoney);
			List productList =  productManager.getObjectList("getObjectYearRate",dataMap);
			if(productList!=null&&productList.size()>0){
				ProductModel productModel = (ProductModel)productList.get(0);
				objJson = new JSONObject();
				BigDecimal  totalfinance = BigDecimal.valueOf(Long.valueOf(totalMoney));
				BigDecimal  periodNum = BigDecimal.valueOf(Long.valueOf(periodDay));
				String yearRate = productModel.getYearRate();
				objJson.put("yearRate", productModel.getYearRate());
				objJson.put("yearCredit",  totalfinance.multiply(periodNum).divide(new BigDecimal(12),0,BigDecimal.ROUND_HALF_UP));
				objJson.put("realIncome", (totalfinance.multiply(new BigDecimal(yearRate)).multiply(periodNum)).divide(new BigDecimal(1200),0,BigDecimal.ROUND_HALF_UP));
			}
			objJson.put("interestEndDate", DateUtil.getMonth(Integer.valueOf(periodDay)));
		}
		objJson.put("interestStartDate", DateUtil.getCurrentDate());
		log.info( objJson.toString());
		this.ajaxJson(response, objJson.toString());
	}	
	
}